package com.chii.colorization.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chii.colorization.R;
import com.chii.colorization.utils.connes.MyUrl;
import com.chii.colorization.utils.connes.OkHttpUtil;
import com.chii.colorization.utils.SHA1Util;
import com.chii.colorization.utils.SPUtil;
import com.chii.colorization.utils.TimeUtils;

import java.io.IOException;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class  RegisterActivity extends AppCompatActivity {
    public static String TAG = RegisterActivity.class.getName();

    @BindView(R.id.register_imageView)
    ImageView register_imageView;
    @BindView(R.id.register_morning)
    TextView register_morning;
    @BindView(R.id.register_username)
    EditText usernameEditText;
    @BindView(R.id.register_email)
    EditText emailEditText;
    @BindView(R.id.register_password)
    EditText passwordEditText;
    @BindView(R.id.register)
    Button registerButton;
    private Handler mHandler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initHandler();
        //初始化返回按键
        initAction();
        //更改主题
        changeStyles();
    }

    private void initAction() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    private void changeStyles() {
        if (new TimeUtils().getCurrentTime()) {
//            Toast.makeText(this, "晚上", Toast.LENGTH_SHORT).show();
            register_imageView.setImageResource(R.drawable.good_night_color);
            register_morning.setText("Night");
        } else {
//            Toast.makeText(this, "白天", Toast.LENGTH_SHORT).show();
            register_imageView.setImageResource(R.drawable.good_morning_color);
        }
    }

    @OnClick(R.id.register)
    public void onViewClicked() {
        registerButton.setEnabled(false);
        String username = usernameEditText.getText().toString();
        String email = emailEditText.getText().toString();
        String password = passwordEditText.getText().toString();
        String finalPassword = SHA1Util.getSHA(password);
        SPUtil.put(RegisterActivity.this, "username", username);
        SPUtil.put(RegisterActivity.this, "password", password);
        SPUtil.put(RegisterActivity.this, "finalPassword", finalPassword);
        //url访问以及密码加密
        HashMap<String, String> map = new HashMap<>();
        map.put("username", username);
        map.put("password", finalPassword);
        map.put("email", email);
        register(map);
    }

    private void register(HashMap<String, String> map) {
        OkHttpUtil okHttpUtil = OkHttpUtil.getInstance(this);
        okHttpUtil.post(MyUrl.register, map, new OkHttpUtil.MyCallback() {
            @Override
            public void success(String s) {
                System.out.println(s);
                JSONObject jsonObject = (JSONObject) JSON.parse(s);
                if (jsonObject.containsKey("code")) {
                    Message message = new Message();
                    message.what = jsonObject.getInteger("code");
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void failed(IOException e) {
                Message message = new Message();
                message.what = 500;
                mHandler.sendMessage(message);
            }
        });
    }
    private void initHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                registerButton.setEnabled(true);
                switch (msg.what) {
                    case 200:
                        Intent intent = new Intent();
                        intent.putExtra("registered", true);
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                        break;
                    case 2008:
                        Toast.makeText(RegisterActivity.this, "账号已存在，请重试",
                                Toast.LENGTH_SHORT).show();
                        break;
                    default:
                }
                return false;
            }
        });
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

}
